﻿using System;
using UnityEngine;

namespace HEFramework
{
    public static class DataTableExtension
    {
        public static string[] ParseStringArr(string _text)
        {
            string[] texts = _text.Split(DataTableSettingScriptableObject.Instance.PropertyArrSplitChar);
            if (texts.Length == 1 && texts[0] == "")
            {
                return new string[0];
            }
            else
            {
                return texts;
            }
        }

        public static TEnum[] ParseEnumArr<TEnum>(string _text) where TEnum : struct
        {
            string[] texts = ParseStringArr(_text);
            int length = texts.Length;
            TEnum[] arr = new TEnum[length];
            for (int i = 0; i < length; i++)
            {
                arr[i] = Enum.Parse<TEnum>(texts[i]);
            }

            return arr;
        }

        public static int[] ParseIntArr(string _text)
        {
            string[] texts = ParseStringArr(_text);
            int length = texts.Length;
            int[] arr = new int[length];
            for (int i = 0; i < length; i++)
            {
                arr[i] = int.Parse(texts[i]);
            }

            return arr;
        }

        public static float[] ParseFloatArr(string _text)
        {
            string[] texts = ParseStringArr(_text);
            int length = texts.Length;
            float[] arr = new float[length];
            for (int i = 0; i < length; i++)
            {
                arr[i] = float.Parse(texts[i]);
            }

            return arr;
        }

        public static double[] ParseDoubleArr(string _text)
        {
            string[] texts = ParseStringArr(_text);
            int length = texts.Length;
            double[] arr = new double[length];
            for (int i = 0; i < length; i++)
            {
                arr[i] = double.Parse(texts[i]);
            }

            return arr;
        }

        public static bool[] ParseBoolArr(string _text)
        {
            string[] texts = ParseStringArr(_text);
            int length = texts.Length;
            bool[] arr = new bool[length];
            for (int i = 0; i < length; i++)
            {
                arr[i] = bool.Parse(texts[i]);
            }

            return arr;
        }

        public static Vector2 ParseVector2(string _text)
        {
            string[] temp = _text.Split(",");
            float x, y;
            if (temp.Length == 2 &&
                float.TryParse(temp[0], out x) &&
                float.TryParse(temp[1], out y))
            {
                return new Vector2(x, y);
            }
            else
            {
                return Vector2.zero;
            }
        }

        public static Vector2[] ParseVector2Arr(string _text)
        {
            string[] texts = ParseStringArr(_text);
            int length = texts.Length;
            Vector2[] arr = new Vector2[length];
            for (int i = 0; i < length; i++)
            {
                arr[i] = ParseVector2(texts[i]);
            }

            return arr;
        }

        public static string GetStringFromKey(string _key)
        {
            if (_key == "")
            {
                return "";
            }
            else
            {
                // 多语言加载内容
                return DataTableLoader.Instance.GetLocalizationString(_key);
            }
        }
    }
}